<?php
/**
 * hddBackup
 *
 * backup class for physical files/folders
 *
 * @category   Backup Types
 * @package    s3Cinch v1.0
 * @author     Stephen Gray <stephen@zgsolutions.co.uk>
 * @copyright  2010 Stephen Gray
 * @since      Sun 10 Jan 10 09:33
 */
class hddBackup extends s3CinchBaseBackup
{
    /**
     * void createArchive ( )
     *
     * archive up set path of file/folder
     *
     * @return void
     *
     * @throws s3ArchiveException
     * @access public
     */
    public function createArchive()
    {
        global $config;
        if (is_dir($this->_mainData[1]))
        {
            $type = 'folder';
        }
        elseif (is_file($this->_mainData[1]))
        {
            $type = 'file';
        }
        else
        {
            throw new s3ArchiveException('Invalid path, must be an existing file or folder: '.$this->_mainData[1]);
        }

        $this->log('Creating archive for '.$type.' '.$this->_mainData[1]);

        /**
         * make a folder for this in the archives
         */
        $fileName = end(explode('/', $this->_mainData[1]));
        $this->_bucketFileFolder = $fileName;
        $this->_checkForFolder($fileName);

        /**
         * if prefix exists ($this->_mainData[2])
         * use it in the filename
         */
        if (isset($this->_mainData[2]) && !empty($this->_mainData[2]))
        {
            $fileName = $this->_mainData[2].'-'.$fileName;
            $this->log('Applying prefix to filename: '.$fileName);
        }        

        /**
         * the command we're going to run
         */
        $archiveFileName = $config['dirs']['archives'].'/'.$this->_bucketFileFolder.'/'.date($config['general']['file_date_format']).'-'.$fileName.'.tar.gz';
        $command = str_replace('{target}', $archiveFileName, $config['general']['archive_file_command']);
        $command = str_replace('{source}', $this->_mainData[1], $command);
        $this->log('Command: '.$command);

        /**
         * run it
         */
        $output = shell_exec($command);
        if (!empty($output))
        {
            throw new s3ArchiveException('Error running archive command: '.$output);
        }
        else
        {
            $this->log('Successfully created file '.$archiveFileName);
            $this->_archiveFilePath = $archiveFileName;
        }
    }
}
